﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Windows.Forms;

namespace TheQuest
{
    class Player :Mover
    {
        private Weapon equippedWeapon;

        public int HitPoints { get; private set; }  

        private List<Weapon> inventory = new List<Weapon>();

        public IEnumerable<string> Weapons
        {
            get
            {
                List<string> names = new List<string>();
                foreach (Weapon weapons in inventory) names.Add(weapons.Name);
                return names;
            }
        }
        public Player(Game game, Point location)
            : base(game, location)
        {
            HitPoints = 10;
        }

        public void Hit(int maxDamage, Random random)
        {
            HitPoints -= random.Next(1, maxDamage);
        }

        public void IncreaseHealth(int healt, Random random)
        {
            HitPoints += random.Next(1, healt);
        }

        public void Equip(string weaponName)
        {
            foreach (Weapon weapon in inventory)
            {
                if (weapon.Name == weaponName)
                {
                    equippedWeapon = weapon;
                }
            }
        }

        public void Move(Direction direction)
        {   
            base.location = Move(direction, game.Bounduaries);
            if (!game.WeaponInRoom.PickedUp)
            {
               // Add code here
                if (game.WeaponInRoom.Nearby(this.location,1))
                {
                    game.WeaponInRoom.PickUpWeapon();
                    
                    inventory.Add(game.WeaponInRoom);
                    if (inventory.Count == 1)
                    {
                        Equip(game.WeaponInRoom.Name);
                    }
                }
            }
        }

        public void Attack(Direction direction, Random random)
        {
            if (equippedWeapon != null)
            {
                equippedWeapon.Attack(direction, random);
                if (equippedWeapon is IPotion)
                {
                    inventory.Remove(equippedWeapon);
                    if (inventory.Count != 0)
                    {
                        equippedWeapon = inventory[0];
                        Equip(equippedWeapon.Name);
                    }
                    else equippedWeapon = null;
                }
            }
        }
    }
}
